from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView
from .models import *
from .courseserializer import *


# Create your views here.


# 课程接口
class CourseAPIView(APIView):
    def get(self, request):
        cate_list = CourseCategoryModel.objects.all()
        course_list = CourseModel.objects.all()

        # 序列化展示
        cate_list = ShowCourseSerializer(cate_list, many=True).data
        course_list = ShowCouSerializer(course_list, many=True).data



        return Response({'code': 200, 'msg': '展示成功', 'cate_list': cate_list, 'course_list': course_list})



    def post(self, request):
        pass


# 查询
class CourseCondition(APIView):
    def post(self, request):
        pay_type = request.data.get('paytype')
        cate_type = request.data.get('catetype')
        # 全部为0
        if not pay_type and not cate_type:
            course = CourseModel.objects.all()
        # 上为0
        elif not pay_type and cate_type:
            course = CourseModel.objects.filter(category=cate_type).all()

        # 下为0
        elif pay_type and not cate_type:
            course = CourseModel.objects.filter(tag=pay_type).all()

        # 其他
        else:
            course = CourseModel.objects.filter(category=cate_type, tag=pay_type).all()

        # 序列化
        course_list = CouSerializer(instance=course, many=True).data

        return Response({'code': 200, 'message': '查询课程', 'courses_data': course_list})


# 章节
class ChapterAPIView(APIView):
    def get(self, request):
        cid = request.query_params.get('cid')

        # 查询章节
        chapter_list = ChapterModel.objects.filter(course=cid).all()
        # 查询课程
        course = CourseModel.objects.filter(id=cid).first()
        # 老师查询
        teacher = TeacherModel.objects.filter(id=course.teacher_id).first()


        # 序列化
        course = CouSerializer(instance=course).data
        chapter_list = ChapterSerializer(instance=chapter_list, many=True).data
        teacher = TeacherSerializer(instance=teacher).data

        return Response({
            'code': 200, 'msg': '查询课程详情', 'chapter': chapter_list
                         ,'course': course, 'teacher': teacher})


